package com.jn.mvvm2.views;

import android.content.Intent;
import android.view.View;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.jn.mvvm2.R;
import com.jn.mvvm2.base.BaseActivity;
import com.jn.mvvm2.databinding.ActivityHanziBinding;
import com.jn.mvvm2.databinding.ActivityNewsBinding;
import com.jn.mvvm2.entity.HanZi;
import com.jn.mvvm2.entity.News;
import com.jn.mvvm2.viewmodel.HanZiViewModel;
import com.jn.mvvm2.viewmodel.NewsViewModel;

public class HanZiActivity extends BaseActivity<ActivityHanziBinding> {

    private HanZiViewModel mHanZiViewModel;
    private LiveData<HanZi> mHanZiLiveData;
    @Override
    public int getLayoutId() {
        return R.layout.activity_hanzi;
    }

    @Override
    public void init() {
        mHanZiViewModel = new ViewModelProvider(this).get(HanZiViewModel.class);
        mHanZiLiveData = mHanZiViewModel.getLiveData();
        mHanZiLiveData.observe(this, new Observer<HanZi>() {
            @Override
            public void onChanged(HanZi hanZi) {
                Glide.with(HanZiActivity.this).asGif().load(hanZi.getAnimation()).into(mBinding.image);
                mBinding.text.setText(hanZi.getBisbun() + "\n(共 " + hanZi.getStroke_count() + " 画)");
                mBinding.text1.setText(hanZi.getPinyin());
            }
        });
        mBinding.setListener(new ButtonClickListener());
    }

    public class ButtonClickListener {
        public void onClick(View view) {
            int id = view.getId();
            if (id == R.id.button) {
                mHanZiViewModel.getData(mBinding.edit.getText().toString());
            }
        }
    }
}
